﻿/*
 * @author: wizardc
 */

#if UNITY_EDITOR

using System;
using System.Collections.Generic;
using Dou.Utils;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Dou.Resource
{
    /// <summary>
    /// 基于 AssetDatabase 的资源加载器
    /// </summary>
    public class AssetDatabaseLoader : IAssetLoader
    {
        public delegate bool GetEditorUsedShortNameDelegate(out string error, out Dictionary<string, string> short2Full);
        public static GetEditorUsedShortNameDelegate getEditorUsedShortName;
        
        private Dictionary<string, string> _short2Full;
        
        private AssetDatabaseLoadOperate _assetDatabaseLoad = new AssetDatabaseLoadOperate();

        public int maxLoadABThread { set; get; }
        public int maxLoadAssetThread
        {
            set => _assetDatabaseLoad.maxLoadThread = value;
            get => _assetDatabaseLoad.maxLoadThread;
        }
        
        public void Init(bool isEncrypt, Action onComplete, AssetUnloadSetting[] unloadSettings)
        {
            if (!getEditorUsedShortName(out var error, out _short2Full))
            {
                throw new Exception($"资源管理器初始化失败：{error}");
            }
            
            onComplete();
        }

        public void LoadAssetBundle(string name, Action<AssetBundle> onComplete, int priority)
        {
        }

        public void LoadAssetBundleSync(string name, Action<AssetBundle> onComplete, int priority)
        {
        }

        public AssetBundle GetAssetBundle(string name)
        {
            return null;
        }

        public void UnloadAssetBundle(string name, bool force)
        {
        }

        public void AddRef(string abName, int count = 1)
        {
        }

        public void DecRef(string abName, int count = 1)
        {
        }

        public void AddAssetRef(string assetName, int count = 1)
        {
        }

        public void DecAssetRef(string assetName, int count = 1)
        {
        }

        public void LoadAsset(string name, Type type, Action<Object> onComplete, int priority)
        {
            name = name.ToLower();
            if (!_short2Full.ContainsKey(name))
            {
                Log.Error($"加载的文件不存在\"{name}\"！");
                return;
            }

            name = _short2Full[name];
            _assetDatabaseLoad.LoadAsset(name, type, onComplete, priority, false);
        }

        public void LoadAssetSync(string name, Type type, Action<Object> onComplete, int priority)
        {
            name = name.ToLower();
            if (!_short2Full.ContainsKey(name))
            {
                Log.Error($"加载的文件不存在\"{name}\"！");
                return;
            }

            name = _short2Full[name];
            _assetDatabaseLoad.LoadAsset(name, type, onComplete, priority, true);
        }
        
        public void UnloadAsset(string name)
        {
        }
        
        public void LoadPrefab(string name, Action<GameObject> onComplete, int priority)
        {
            LoadAsset(name, typeof(GameObject), asset =>
            {
                var go = GameObject.Instantiate((GameObject) asset);
                onComplete(go);
            }, priority);
        }

        public void LoadPrefabSync(string name, Action<GameObject> onComplete, int priority)
        {
            LoadAssetSync(name, typeof(GameObject), asset =>
            {
                var go = GameObject.Instantiate((GameObject) asset);
                onComplete(go);
            }, priority);
        }

        public void UnloadUnusedAB()
        {
        }

        public string GetAssetBundleName(string name)
        {
            return name;
        }

        public string GetAssetFullPath(string name)
        {
            name = name.ToLower();
            if (_short2Full.ContainsKey(name))
            {
                return _short2Full[name];
            }
            return name;
        }

        public string GetRawAssetPath(string name)
        {
            name = name.ToLower();
            if (_short2Full.ContainsKey(name))
            {
                return _short2Full[name];
            }
            return name;
        }
    }
}

#endif
